import Router from 'koa-router'
import damageUpSchema from '../dbs/models/damageUp'
import damageCostSchema from '../dbs/models/damageCost'

let router = new Router({ prefix: '/damage' })

// 上传损坏列表
router.get('/damageUp', async (ctx, next) => {
  let name = ctx.query.name
  let studentNumber = ctx.query.studentNumber
  let Class = ctx.query.class
  let article = ctx.query.article
  let desc = ctx.query.desc
  let tower = ctx.query.tower
  let dorm = ctx.query.dorm
  let tel = ctx.query.tel
  let where = {
    name,
    studentNumber,
    class: Class,
    article,
    tower,
    dorm,
    desc,
    tel
  }
  // console.log(where)
  const damageUp = new damageUpSchema(where)
  ctx.body = {
    where
  }
  let code
  try {
    await damageUp.save()
    code = 0
  } catch (e) {
    code = -1
  }
  ctx.body = {
    meg: code === 0 ? '已提交损坏表格' : '提交失败',
    code: code
  }
})
// 查找损坏列表
router.get('/findDamageUp', async (ctx, next) => {
  let tower = ctx.query.tower
  let page = Number(ctx.query.pagenum)//当前第几页
  let size = Number(ctx.query.pagesize)//每页显示的记录条数
  let where = { tower }
  // console.log(tower, page, size)
  let options = { "limit": size, "skip": (page - 1) * size }
  let result = await damageUpSchema.find(where, null, options)
  let count = await damageUpSchema.find(where, null)
  let total = count.length
  // console.log(result)
  ctx.body = {
    total: total,
    damageUpList: result,
    meta: {
      msg: "请求数据成功",
      code: 200
    }
  }
})
// 查找特定id的损坏上报信息
router.get('/serchFixMan', async (ctx) => {
  let { _id } = ctx.query
  let where = { _id }
  // console.log(_id, where)
  let result = await damageUpSchema.find(where)
  if (result.length !== 0) {
    ctx.body = {
      result,
      meta: {
        msg: '请求成功',
        code: 200
      }
    }
  } else {
    ctx.body = {
      meta: {
        msg: '错误，请联系管理员',
        code: -1
      }
    }
  }
})
// 查询未维修的损坏列表
router.get('/noFix', async (ctx, next) => {
  let tower = ctx.query.tower
  let where = {
    tower,
    isSolve: false
  }
  try {
    let noFixList = await damageUpSchema.find(where, null)
    let noFinshTotal = noFixList.length
    if (noFixList.length) {
      ctx.body = {
        noFixList,
        noFinshTotal,
        meta: {
          msg: '请求成功',
          code: 200
        }
      }
    } else {
      ctx.body = {
        noFixList,
        noFinshTotal,
        meta: {
          msg: '请求成功',
          code: 200
        }
      }
    }
  } catch (error) {
    console.log(error)
    ctx.body = {
      meta: {
        code: 500,
        msg: '服务器错误'
      }
    }
  }
})
// 查询已维修的损坏列表
router.get('/haveBeenFixed', async (ctx, next) => {
  let tower = ctx.query.tower
  let where = {
    tower,
    isSolve: true
  }
  try {
    let haveBeenFixed = await damageUpSchema.find(where, null)
    let finshTotal = haveBeenFixed.length
    if (haveBeenFixed.length) {
      ctx.body = {
        haveBeenFixed,
        finshTotal,
        meta: {
          msg: '请求成功',
          code: 200
        }
      }
    } else {
      ctx.body = {
        haveBeenFixed,
        finshTotal,
        meta: {
          msg: '请求成功',
          code: 200
        }
      }
    }
  } catch (error) {
    console.log(error)
    ctx.body = {
      meta: {
        code: 500,
        msg: '服务器错误'
      }
    }
  }
})
// 完成宿舍损坏维修的接口
router.get('/HBFixed', async (ctx, next) => {
  let _id = { _id: ctx.query._id }
  let isSolve = ctx.query.isSolve
  var tower = ctx.query.tower
  let newfinishTime = {}
  // console.log(ctx.query)
  if (isSolve === 'true') {
    // console.log('True')
    newfinishTime = {
      finishTime: ctx.query.finishTime,
      isSolve: ctx.query.isSolve
    }
    try {
      await damageUpSchema.updateOne(_id, newfinishTime)
      const damageUpList = await damageUpSchema.find({ tower: tower }, null, { "limit": 7, "skip": 0 })
      ctx.body = {
        damageUpList,
        msg: '修改成功',
        code: 0
      }
    } catch (error) {
      if (error) {
        ctx.body = {
          meg: '修改失败',
          code: -1
        }
      }
    }
  } else {
    newfinishTime = {
      finishTime: '',
      isSolve: ctx.query.isSolve
    }
    try {
      await damageUpSchema.updateOne(_id, newfinishTime)
      const damageUpList = await damageUpSchema.find({ tower: tower }, null, { "limit": 7, "skip": 0 })
      ctx.body = {
        damageUpList,
        msg: '修改成功',
        code: 0
      }
    } catch (error) {
      if (error) {
        ctx.body = {
          meg: '修改失败',
          code: -1
        }
      }
    }

  }


})
// 上传已维修费用报表
router.get('/cost', async (ctx) => {
  let { tower, dorm, name, serviceman, repalce, costs, count, damageId } = ctx.query
  let where = { tower, dorm, name, serviceman, repalce, costs, count, damageId }
  const addStudent = new damageCostSchema(where)
  let code
  try {
    await addStudent.save()
    code = 0
  } catch (e) {
    code = -1
  }
  ctx.body = {
    meta: {
      msg: '已上传维修费用报表',
      code: code
    }
  }
})
// 删除上传错误的修费用报表
router.get('/removeCost', async (ctx) => {
  const deleteWhere = { damageId: ctx.query.damageId }
  // console.log(deleteStudent)
  let code
  try {
    await damageCostSchema.remove(deleteWhere, null)
    code = 0
  } catch (e) {
    code = -1
  }
  ctx.body = {
    meta: {
      msg: '已删除上传错误的费用报表',
      code: code
    }
  }
})
// 查看维修费用
router.get('/findCostInfo', async ctx => {
  let where = {
    tower: ctx.query.tower
  }
  let page = Number(ctx.query.pagenum)//当前第几页
  let size = Number(ctx.query.pagesize)//每页显示的记录条数
  let options = { "limit": size, "skip": (page - 1) * size }
  try {
    let result_cost = await damageCostSchema.find(where, null, options)
    let count = await damageCostSchema.find(where, null)
    let total = count.length
    ctx.body = {
      result_cost,
      total: total,
      meta: {
        msg: "请求数据成功",
        code: 200
      }
    }
  } catch (error) {
    ctx.body = {
      meta: {
        msg: "获取数据失败",
        code: 204
      }
    }
  }
})
// 修改维修费用
router.post('/editCots', async ctx => {
  let where = {
    _id: ctx.request.body._id
  }
  let newCostsInfo = {
    serviceman: ctx.request.body.serviceman,
    repalce: ctx.request.body.repalce,
    count: ctx.request.body.count,
    costs: ctx.request.body.costs
  }
  await damageCostSchema.updateOne(where, newCostsInfo, (err, res) => {
    if (err) {
      ctx.body = {
        meta: {
          msg: '修改失败',
          code: -1
        }
      }
    } else if (res.n === 0) {
      ctx.body = {
        meta: {
          msg: '修改失败,没有找到该条数据',
          code: -1
        }
      }
    } else {
      ctx.body = {
        meta: {
          msg: '修改成功',
          code: 0
        }
      }
    }
  })
})
// 获取本宿舍历史上报维修信息
router.get('/currDormDamageInfo', async ctx => {
  let where = {
    tower: ctx.query.tower,
    dorm: ctx.query.dorm
  }
  let result = await damageUpSchema.find(where, null).sort({ 'time': -1 })
  if (result.length !== 0) {
    ctx.body = {
      result,
      meta: {
        msg: "请求数据成功",
        code: 200
      }
    }
  } else {
    ctx.body = {
      meta: {
        msg: "本宿舍暂时没有上报维修哦~",
        code: 201
      }
    }
  }

})
export default router
